package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.db.Log;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class LogListener {
    @Autowired
    private LogMapper logMapper;

    @RabbitListener(
        bindings=@QueueBinding(
                value = @Queue(value = "tanhua.log.queue"),
                exchange = @Exchange(value = "tanhua.log.exchange",type = ExchangeTypes.TOPIC),
                key = "log.*"
        )
    )
    public void log(String message){
        System.out.println("接收到消息" +  message);
        //获取消息
        Map map = JSON.parseObject(message, Map.class);
        String type = map.get("type").toString();
        String logTime = map.get("logTime").toString();
        String s = map.get("userId").toString();
        Long userId = Long.valueOf(s);
        Log log = new Log();
        log.setLogTime(logTime);
        log.setUserId(userId);
        log.setType(type);
        logMapper.insert(log);



    }

}
